package dao.impl;

import dao.ArticleDao;
import domain.Article;
import domain.ArticleLabel;
import domain.Label;
import util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author: 爱学Java 不想秃头的 KK
 * @date: 2023/6/28 20:52
 * @version: 1.0
 * @Created with IntelliJ IDEA.
 * @Description:
 */
public class ArticleDaoImpl implements ArticleDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public int findTotalCount(Map<String, String[]> condition) {
        //1.定义模版sql
        String sql = "select count(*) from tab_article where 1=1 ";
        StringBuffer buffer = new StringBuffer(sql);
        //2.拼接sql语句
        Set<String> keySet = condition.keySet(); //该方法返回map中所有键的值的列表。
        //定义参数的集合
        List<Object> params = new ArrayList<>();
        for (String key : keySet) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            //获取value的值
            String value = condition.get(key)[0];
            //判断value中是否有值
            if (!"lid".equals(value)) { //排除lid只在一张表中条件查询
                if (value != null && !"".equals(value)) {
                    buffer.append(" and " + key + " like ?");
                    params.add("%" + value + "%"); //"?"条件的值
                }
            }
        }
        buffer.append(" and visible = ? and recycle = ? ");
        params.add("Y");
        params.add("N");
        System.out.println(buffer.toString());
        System.out.println(params);
        return template.queryForObject(
                buffer.toString(),
                Integer.class,//java 1.5特性会把返回的Integer类型的数据自动拆箱为int类型
                params.toArray()
        );
    }

    @Override
    public List<Article> findByPage(int start, int rows, Map<String, String[]> condition) {
        //1.定义原生sql
        String sql = "select * from tab_article where 1=1 ";
        StringBuffer buffer = new StringBuffer(sql);
        //2.拼接sql语句
        Set<String> keySet = condition.keySet(); //该方法返回map中所有键的值的列表。
        //定义参数的集合
        List<Object> params = new ArrayList<>();
        for (String key : keySet) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            //获取value的值
            String value = condition.get(key)[0];
            //判断value中是否有值
            if (value != null && !"".equals(value)) {
                buffer.append(" and " + key + " like ?");
                params.add("%" + value + "%"); //"?"条件的值
            }
        }
        //添加分页查询
        buffer.append(" and visible = ? and recycle = ? ");
        buffer.append(" limit ?, ? ");
        params.add("Y");
        params.add("N");
        params.add(start);
        params.add(rows);
        System.out.println(buffer.toString());
        System.out.println(params);
        return template.query(
                buffer.toString(),
                new BeanPropertyRowMapper<Article>(Article.class),
                params.toArray()
        );
    }

    @Override
    public int findLabelTotalCount(int lid) {
        String sql = "select count(*) from tab_article_label where lid = ?";
        return template.queryForObject(sql, Integer.class, lid);
    }

    @Override
    public List<ArticleLabel> findLabelPageBean(int start, int rows, int lid) {
        String sql = "select *from tab_article_label where lid = ? limit ?, ?";
        return template.query(sql,
                new BeanPropertyRowMapper<ArticleLabel>(ArticleLabel.class),
                lid,
                start,
                rows
        );
    }


    @Override
    public Article findArticleByRid(int rid) {
        String sql = "select *from tab_article where rid = ? and visible = ? and recycle = ?";
        return template.queryForObject(sql,
                new BeanPropertyRowMapper<Article>(Article.class),
                rid,
                "Y",
                "N"
        );
    }

    @Override
    public int findTotalCountFromLabel() {
        String sql = "select count(*) from tab_category";
        return template.queryForObject(sql, Integer.class);
    }

    @Override
    public List<Label> findByPageFromLabel(int start, int rows) {
        String sql = "select * from tab_label limit ?, ?";
        return template.query(
                sql,
                new BeanPropertyRowMapper<Label>(Label.class),
                start,
                rows
        );
    }

    @Override
    public int findGoodArticleTotalCount() {
        String sql = "select count(*) from tab_article where visible = ? and recycle = ? and favour >= ?";
        return template.queryForObject(sql,
                Integer.class,
                "Y",
                "N",
                100
        );
    }

    @Override
    public List<Article> findByPageFromGoodArticle(int start, int rows) {
        String sql = "select * from tab_article where visible = ? and recycle = ? and favour >= ? limit ?, ?";

        return template.query(
                sql,
                new BeanPropertyRowMapper<Article>(Article.class),
                "Y",
                "N",
                100,
                start,
                rows
                );
    }

    @Override
    public void addBrowserNumByRid(int rid) {
        String sql = "UPDATE tab_article SET browserNum = browserNum + 1 WHERE rid = ?";
        template.update(sql, rid);
    }
}
